package base_demo.moneyTest;

import java.util.Random;

public class MyThread extends Thread {
    static double money = 100;
    //最大金额
    static final double MAX = 99.80;
    //最小金额
    static final double MIN = 0.01;
    //红包个数
    static int count = 3;


    //同步代码块
    //判断是否到了末尾
    @Override
    public void run() {

        synchronized (MyThread.class) {
            if (count == 0) {
                //最后一个红包了
                System.out.println(getName() + "没有抢到红包");
            } else {
                double prize = 0;
                if (count == 1) {
                    prize = money;
                } else {
                    Random r = new Random();
                    double bounds  = money - (count -1) * MIN;
                    prize = r.nextDouble(bounds);
                    if(prize < MIN){
                        prize = MIN;
                    }

                }
                System.out.println(getName() + "抽到了" + prize + "元");
                money -= prize;
                count--;

            }

        }
    }
}
